#!/usr/bin/env python
# -*- coding:utf8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def init(db):

    db.drop_all()
    db.create_all()

    with open('tools/init.sql') as f:
        lines = f.readlines()
        print len(lines)
        for line in lines:
            #print "line:{0}".format(line)
            try:
                r = db.session.execute(line)

                db.session.commit()
                print "execute rows:%d" % r.rowcount
            except Exception,e:
                print "error:%s" % e.message
                continue
        print "this is the file end."
    f.close()

    init_clips(db)

def init_clips(db):
    from app.clips.models import Category,Tag 

    for cat in ("cmd","path","sql","conf","conn","keymap"):
        category = Category(category_name=cat)
        db.session.add(category)

    for t in ("java","maven","oracle","pip","npm","sqlserver","mysql","git","subversion","yarn","nginx"):
        tag = Tag(name=t,status=0)
        db.session.add(tag)

    #How to Load java and then add to tag_urls, when dirty available?
    db.session.commit()
